package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyRepairManageService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 *       报修数据操作完成付款消息队列消费者
 * </desc>
 *
 * @createDate 2018/08/13
 */
@Component
public class PropertyRepairCompletePayConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyRepairCompletePayConsumer.class); //日志对象

    @Autowired
    private IPropertyRepairManageService iPropertyRepairManageService;

    /**
     * <desc>
     *      非核心服务-物业报修完成付款消费过程
     * </desc>
     *
     * @param message 监听到的业务消息
     * @param channel 频道
     * @throws InterruptedException
     * @throws IOException
     * @author wangzhi
     * @createDate 2018/08/13
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_REPAIR_MANAGER_COMPLETE_PAY_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            String feeId = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent()).getString("data");
            if (StringUtils.isNotBlank(feeId)) {
                //LOG.info("【RabbitMQ_非核心服务-报修数据操作完成付款】获取到缴费账单feeId = " + feeId);
                iPropertyRepairManageService.completePay(feeId);
            } else {
                LOG.info("【RabbitMQ_非核心服务-报修数据操作完成付款】未获取到缴费账单feeId");
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_非核心服务-报修数据操作完成付款】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

}
